#!/usr/bin/bash

IMAGE_INFO="/usr/share/ublue-os/image-info.json"
BASE_IMAGE_NAME=$(jq -r '."base-image-name"' < $IMAGE_INFO)

USER=$(id -nu 1000)
HOME=$(getent passwd $USER | cut -d: -f6)

# SteamOS SDDM config
SDDM_CONF='/etc/sddm.conf.d/steamos.conf'
AUTOLOGIN_CONF='/etc/sddm.conf.d/zz-steamos-autologin.conf'
DESKTOP_AUTOLOGIN='/etc/bazzite/desktop_autologin'

# Avoid autologin conflict
if [[ -f ${AUTOLOGIN_CONF} ]]; then
  rm -f ${AUTOLOGIN_CONF}
fi

# Configure autologin if Steam has been updated
if [[ ! -f ${DESKTOP_AUTOLOGIN} ]]; then
  sed -i 's/.*Session=.*/Session=gamescope-session.desktop/g' ${SDDM_CONF}
elif [[ ${BASE_IMAGE_NAME} =~ "kinoite" ]]; then
  sed -i 's/.*Session=.*/Session=plasma.desktop/g' ${SDDM_CONF}
elif [[ ${BASE_IMAGE_NAME} =~ "silverblue" ]]; then
  sed -i 's/.*Session=.*/Session=gnome-wayland.desktop/g' ${SDDM_CONF}
fi
sed -i 's/.*User=.*/User='${USER}'/g' ${SDDM_CONF}
